package czk.concrent.threadpool.timer;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 定时任务
 * @Author:ChenZhangKun
 * @Date: 2020/7/8 23:04
 */
public class FixTimeTask {
    public static void main(String[] args) {
        // 创建线程池
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
        // 时间间隔
        long period=1000*60*60*24*7;
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        // 获取周四时间
        LocalDateTime time = now.withHour(18).withMinute(0).withSecond(0).withNano(0).with(DayOfWeek.THURSDAY);

        // 如果是周五，则加一周
        if (now.compareTo(time)>0){
            time=time.plusWeeks(1);
        }
        System.out.println(time);
        // 延时时间
        long initialDelay = Duration.between(now, time).toMillis();
        pool.scheduleAtFixedRate(()->{

        },initialDelay,period, TimeUnit.SECONDS);
    }
}
